package com.huatai.manage.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huatai.common.util.BeanUtil;
import com.huatai.common.util.JsonUtil;
import com.huatai.common.util.ObjectUtil;
import com.huatai.common.util.StringUtil;
import com.huatai.manage.entity.TbTrialSchemeEntity;
import com.huatai.manage.mapper.TbTrialSchemeMapper;
import com.huatai.manage.query.PageQuery;

import com.huatai.manage.service.TbTrialSchemeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.manage.utils.ComUtil;
import com.huatai.manage.vo.PageVo;
import com.huatai.manage.vo.ResultVo;
import com.huatai.manage.vo.TbTrialSchemeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 模板表; 服务实现类
 * </p>
 *
 * @author luoxc
 * @since 2023-03-16
 */
@Service
public class TbTrialSchemeServiceImpl extends ServiceImpl<TbTrialSchemeMapper, TbTrialSchemeEntity> implements TbTrialSchemeService {

	@Autowired
	private TbTrialSchemeMapper mapper;

	/**
	 * 分页获取方案列表
	 *
	 * @param pageQuery
	 * @return
	 */
	@Override
	public PageVo pageList(PageQuery<TbTrialSchemeEntity> pageQuery) {
		Page<TbTrialSchemeEntity> page = new Page<>(pageQuery.getCurrent(), pageQuery.getSize());
		/*LambdaQueryWrapper<TbTrialSchemeEntity> wrapper = Wrappers.<TbTrialSchemeEntity>lambdaQuery();
		wrapper.like(StringUtil.isNotBlank(pageQuery.getData().getName()), TbTrialSchemeEntity::getName, pageQuery.getData().getName());
		Page<TbTrialSchemeEntity> schemeEntityPage = this.page(page, wrapper);
		List<TbTrialSchemeVo> tbTrialSchemeVos = new ArrayList<>();
		schemeEntityPage.getRecords().forEach(record -> {
			TbTrialSchemeVo tbTrialSchemeVo = JsonUtil.convertValue(record, TbTrialSchemeVo.class);
			tbTrialSchemeVo.setComponents(JSONUtil.parseArray(record.getComponents()));
			tbTrialSchemeVo.setSize(JSONUtil.parseObj(record.getSize()));
			tbTrialSchemeVos.add(tbTrialSchemeVo);
		});*/
		List<TbTrialSchemeVo> schemeVos = this.mapper.pageList(page, pageQuery.getData());
		schemeVos = voToJson(schemeVos);
		PageVo pageVo = BeanUtil.copyProperties(page, PageVo.class);
		pageVo.setRecords(schemeVos);
		return pageVo;
	}

	/**
	 * @return
	 * @Description 查询所有方案母版
	 * @Date 2023/4/5 13:45
	 * @Param
	 */
	@Override
	public List<TbTrialSchemeVo> schemeList() {
		/*List<TbTrialSchemeVo> tbTrialSchemeVos = new ArrayList<>();
		List<TbTrialSchemeEntity> list = list();
		if (ComUtil.listIsEmpty(list)) return null;
		list.forEach(trialScheme -> {
			TbTrialSchemeVo tbTrialSchemeVo = JsonUtil.convertValue(trialScheme, TbTrialSchemeVo.class);
			tbTrialSchemeVo.setComponents(JSONUtil.parseArray(trialScheme.getComponents()));
			tbTrialSchemeVo.setSize(JSONUtil.parseObj(trialScheme.getSize()));
			tbTrialSchemeVos.add(tbTrialSchemeVo);
		});*/
		List<TbTrialSchemeVo> schemeVos = this.mapper.list();
		return voToJson(schemeVos);
	}

	/**
	 * @return List<ResultVo>
	 * @Description 根据试验id查询（壹瞰思）
	 * @Date 2023/4/4 9:49
	 * @Param
	 */
	@Override
	public List<ResultVo> getByTrialId(String trialId) {
		List<ResultVo> resultVos = new ArrayList<>();
		List<TbTrialSchemeEntity> list = this.list(Wrappers.<TbTrialSchemeEntity>lambdaQuery().eq(TbTrialSchemeEntity::getTrialId, trialId));
		if (list.isEmpty()) return null;
		list.forEach(i -> {
			Map<String, Object> map = new HashMap<>();
			map.put("schemeId", i.getSchemeId());
			resultVos.add(new ResultVo(map, i.getName()));
		});
		return resultVos;
	}


	/**
	 * 根据方案id获取信息
	 *
	 * @param schemeId
	 * @return
	 */
	@Override
	public TbTrialSchemeVo getBySchemeId(String schemeId) {
		/*TbTrialSchemeEntity trialSchemeEntity = getOne(Wrappers.<TbTrialSchemeEntity>lambdaQuery().eq(TbTrialSchemeEntity::getSchemeId, schemeId));
		TbTrialSchemeVo tbTrialSchemeVo = JsonUtil.convertValue(trialSchemeEntity, TbTrialSchemeVo.class);
		tbTrialSchemeVo.setSize(JSONUtil.parseObj(trialSchemeEntity.getSize()));
		tbTrialSchemeVo.setComponents(JSONUtil.parseArray(trialSchemeEntity.getComponents()));*/
		TbTrialSchemeVo schemeVo = this.mapper.getBySchemeId(schemeId);
		if (ObjectUtil.isNotEmpty(schemeVo)){
			schemeVo.setSize(JSONUtil.parseObj(schemeVo.getSize()));
			schemeVo.setComponents(JSONUtil.parseArray(schemeVo.getComponents()));
		}
		return schemeVo;
	}

	/**
	 * @return
	 * @Description 根据试验id分页查询面案列表
	 * @Date 2023/4/5 13:46
	 * @Param pageQuery
	 */
	@Override
	public PageVo pageByTrialId(PageQuery<TbTrialSchemeEntity> pageQuery) {
		return pageList(pageQuery);
		/*Page<TbTrialSchemeEntity> page = new Page<>(pageQuery.getCurrent(), pageQuery.getSize());
		LambdaQueryWrapper<TbTrialSchemeEntity> wrapper = Wrappers.<TbTrialSchemeEntity>lambdaQuery();
		wrapper.eq(TbTrialSchemeEntity::getTrialId, pageQuery.getData().getTrialId());
		List<TbTrialSchemeEntity> schemes = this.page(page, wrapper).getRecords();
		List<TbTrialSchemeVo> tbTrialSchemeVos = new ArrayList<>();
		if (!schemes.isEmpty()) {
			schemes.forEach(scheme -> {
				TbTrialSchemeVo tbTrialSchemeVo = JsonUtil.convertValue(scheme, TbTrialSchemeVo.class);
				tbTrialSchemeVo.setComponents(JSONUtil.parseArray(scheme.getComponents()));
				tbTrialSchemeVo.setSize(JSONUtil.parseObj(scheme.getSize()));
				tbTrialSchemeVos.add(tbTrialSchemeVo);
			});
		}
		PageVo pageVo = BeanUtil.copyProperties(page, PageVo.class);
		return pageVo.setRecords(tbTrialSchemeVos);*/
	}

	/**
	 * @return
	 * @Description 根据试验id查询面案列表
	 * @Date 2023/4/5 13:46
	 * @Param
	 */
	@Override
	public List<TbTrialSchemeVo> listByTrialId(String trialId) {
		/*LambdaQueryWrapper<TbTrialSchemeEntity> wrapper = Wrappers.<TbTrialSchemeEntity>lambdaQuery();
		wrapper.eq(TbTrialSchemeEntity::getTrialId, trialId);
		List<TbTrialSchemeEntity> schemes = this.list(wrapper);
		List<TbTrialSchemeVo> tbTrialSchemeVos = new ArrayList<>();
		if (!schemes.isEmpty()) {
			schemes.forEach(scheme -> {
				TbTrialSchemeVo tbTrialSchemeVo = JsonUtil.convertValue(scheme, TbTrialSchemeVo.class);
				tbTrialSchemeVo.setComponents(JSONUtil.parseArray(scheme.getComponents()));
				tbTrialSchemeVo.setSize(JSONUtil.parseObj(scheme.getSize()));
				tbTrialSchemeVos.add(tbTrialSchemeVo);
			});
		}
		return tbTrialSchemeVos;*/
		List<TbTrialSchemeVo> tbTrialSchemeVos = this.mapper.listByTrialId(trialId);
		return voToJson(tbTrialSchemeVos);
	}

	/**
	 * 新增方案信息
	 *
	 * @param tbTrialSchemeEntity
	 * @return
	 */
	@Override
	public boolean insert(TbTrialSchemeEntity tbTrialSchemeEntity) {
		Object components = tbTrialSchemeEntity.getComponents();
		Object size = tbTrialSchemeEntity.getSize();
		if (ObjectUtil.isNotEmpty(components)) {
			tbTrialSchemeEntity.setComponents(JSONUtil.parseArray(tbTrialSchemeEntity.getComponents()).toString());
		}
		if (ObjectUtil.isNotEmpty(size)) {
			tbTrialSchemeEntity.setSize(JSONUtil.parseObj(tbTrialSchemeEntity.getSize()).toString());
		}
		return save(tbTrialSchemeEntity.setSchemeId(ComUtil.getUUID()));
	}

	/**
	 * 根据方案id修改数据
	 *
	 * @param tbTrialSchemeEntity
	 * @return
	 */
	@Override
	public boolean update(TbTrialSchemeEntity tbTrialSchemeEntity) {
		if (StringUtil.isEmpty(tbTrialSchemeEntity.getSchemeId())) return false;
		Object components = tbTrialSchemeEntity.getComponents();
		Object size = tbTrialSchemeEntity.getSize();
		if (ObjectUtil.isNotEmpty(components)) {
			tbTrialSchemeEntity.setComponents(JSONUtil.parseArray(tbTrialSchemeEntity.getComponents()).toString());
		}
		if (ObjectUtil.isNotEmpty(size)) {
			tbTrialSchemeEntity.setSize(JSONUtil.parseObj(tbTrialSchemeEntity.getSize()).toString());
		}
		LambdaUpdateWrapper<TbTrialSchemeEntity> wrapper = Wrappers.<TbTrialSchemeEntity>lambdaUpdate()
			.eq(TbTrialSchemeEntity::getSchemeId, tbTrialSchemeEntity.getSchemeId());
		return this.update(tbTrialSchemeEntity, wrapper);
	}

	/**
	 * 根据方案id集合删除数据
	 *
	 * @param schemeIds
	 * @return
	 */
	@Override
	public boolean removes(List<String> schemeIds) {
		if (schemeIds.isEmpty()) return false;
		return remove(Wrappers.<TbTrialSchemeEntity>lambdaQuery().in(TbTrialSchemeEntity::getSchemeId, schemeIds));
	}

	/**
	 * 将components 和 size转换成 json
	 * @param list
	 * @return
	 */
	public List<TbTrialSchemeVo> voToJson(List<TbTrialSchemeVo> list){
		if (ComUtil.listIsNotEmpty(list)) {
			list.forEach(vo -> {
				vo.setComponents(JSONUtil.parseArray(vo.getComponents()));
				vo.setSize(JSONUtil.parseObj(vo.getSize()));
			});
		}
		return list;
	}

}
